<template>
    <view>
      <view class="header-box">
        <view class="header" :style="{paddingTop: (useAppData.titleHeight+3) + 'px'}">
          <view class="back-box" style="left: 0;" @tap="gotoBack">
            <image class="back-img" src="../../static/icon-back.png"></image>
          </view>
          <span>里程记录</span>
        </view>
        <view class="record-tip">里程记录从9月9日开始重新计算</view>
        <view class="con-box">
          <div class="item-box">
            <p class="item-num">{{state.recordObj?.distanceTotal || 0.000}}</p>
            <p class="item-name">总里程 (公里)</p>
          </div>
          <div class="item-box">
            <p class="item-num">{{getTime(state.recordObj?.durationTotal || 0, "hour")}}</p>
            <p class="item-name">总时长</p>
          </div>
        </view>
      </view>
      <view class="record-box" v-if="state.recordObj?.list?.length > 0">
        <ul>
          <li class="record-item" v-for="(item, index) in state.recordObj?.list" :key="index" @tap="redirect(item)">
            <view class="left-content">
              <p class="record-time">{{item.createTime}}</p>
              <div class="record-con">
                <p>里程：{{item.distance || 0.000}}公里</p>
                <p class="record-len">时长：{{getTime(item.duration || 0, "hour")}}</p>
              </div>
            </view>
            <image v-if="item.pathImg" class="arrow" src="../../static/more@2x.png"></image>
          </li>
        </ul>
      </view>
      <view v-else class="no-record">
        <image class="no-img" src="../../static/no-record.png"></image>
        <text>无记录</text>
      </view>
    </view>
</template>

<script setup>
  import { onLaunch, onShow, onHide, onUnload, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";
  import { reactive, onMounted } from "vue"
  import * as API from "../../api/api"
  import { getTime } from "../../utils/util"
  import { storeToRefs } from "pinia";
  import { useRunInfoStore } from "../../store/runInfo";
  import { useAppStore } from "../../store/app";
  import { useShareStore } from "../../store/share";
  const useShareData = useShareStore()
  const useAppData = useAppStore()
  const useRunInfoData = useRunInfoStore()
  const state = reactive({
    list: [
      {
        name: "开始跑步",
        url: "/pages/index/index"
      },
      {
        name: "个人资料",
        url: "/pages/index/index"
      },
      {
        name: "里程记录",
        url: "/pages/index/index"
      },
      {
        name: "来康健康",
        rightText: "小程序"
      },
    ],
    show: false,
    recordObj: {
      distanceTotal: 0,
      durationTotal: 0,
      list: []
    },
    userInfo: null
  })

  onShareAppMessage((res) => {
    return useShareData.shareInfo
  })
  onShareTimeline((res) => {
    return useShareData.shareInfo
  })

  const gotoBack = () => {
    uni.navigateBack()
  }

  const gotoChat = (url) => {
    uni.navigateTo({
      url: "/pages/chat/chat"
    });
  }

  const handleListMileage = () => {
    useRunInfoData.onListMileage().then(res => {
      state.recordObj = {
        ...res
      }
      console.log("state.recordObj", state.recordObj)
    })
  }

  const redirect = (item) => {
    console.log(item)
    // if (!item.pathImg) return
    uni.navigateTo({
      url: `/examPages/record/detail?id=${item.userId}&uuid=${item.uuid}`
    });
  }


  onShow(() => {
    state.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : null
    useRunInfoData.setProportionShow(0.7)
    useRunInfoData.setInitStatus(true)
    handleListMileage()
  })
</script>

<style lang="scss" scoped>
  .record-tip{
    width: 392rpx;
    height: 48rpx;
    background: rgba(230,255,242,0.67);
    border-radius: 24rpx;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 24rpx;
    color: rgba(34,34,34,0.8);
    line-height: 48rpx;
    text-align: center;
    margin: 35rpx auto 0;
  }
  .con-box{
    display: flex;
    justify-content: space-around;
    margin-top: 35rpx;
    .item-box{
      color: #222222;
      text-align: center;
      .item-num{
        font-weight: 600;
        font-size: 44rpx;
      }
      .item-name{
        font-size: 36rpx;
      }
    }
  }
  .record-box{
    height: calc(100vh - 406rpx);
    padding-bottom: 100rpx;
    box-sizing: border-box;
    overflow: auto;
    .record-item{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 32rpx 32rpx 30rpx;
      background: #FFFFFF;
      box-shadow: inset 0rpx -2rpx 0rpx 0rpx #E6E8EF;
      box-sizing: border-box;
      .left-content{
        flex: 1;
        flex-shrink: 0;
        .record-time{
          font-size: 24rpx;
          color: #585858;
        }
        .record-con{
          font-size: 28rpx;
          color: #222222;
          display: flex;
          margin-top: 12rpx;
        }
        .record-len{
          margin-left: 32rpx;
        }
      }
      .arrow{
        width: 32rpx;
        height: 32rpx;
      }
    }
  }
  .no-record{
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 40rpx;
    color: #222222;
    padding-top: 20vh;
    .no-img{
      width: 200rpx;
      height: 176rpx;
      margin-bottom: 36rpx;
    }
  }
  .header-box{
    width: 100vw;
    height: 406rpx;
    text-align: center;
    background: url('') 0 0 no-repeat;
    background-size: 100% 100%;
    .title{
      font-size: 34rpx;
      color: #000000;
    }
  }
</style>
